package freecomm.study.servicetestclient;

import freecomm.study.servicetest.IPerson;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    private IPerson iperson;
    private TextView tv1;
    private Button button1;
    protected static final String SERVICE_ACTION = "freecomm.study.servicetest.intent.action.SERVICE_ACTION";
    private ServiceConnection conn = new ServiceConnection()
    {

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            // TODO Auto-generated method stub
            
        }

        @Override
        synchronized public void onServiceConnected(ComponentName name, IBinder service)
        {
            // TODO Auto-generated method stub
            iperson = IPerson.Stub.asInterface(service);
            if(iperson!=null){
                try{
                    iperson.setName("Junwei.xu");
                    iperson.setAge(23);
                    String msg = iperson.display();
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                }catch(RemoteException e){
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv1 = (TextView) findViewById(R.id.tv1);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setAction(SERVICE_ACTION);
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        });
    }
}